﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Web;
using System.Xml;

namespace YXShop.TemplateAction
{
    public class CustomLabel : Template
    {
        /// <summary>
        /// 标签内容
        /// </summary>
        private string LabelContent = string.Empty;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="labelname">标签名称</param>
        public CustomLabel(string labelname, string labeltype)
            : base(labelname, labeltype)
        {
        }
        /// <summary>
        /// 从数据库获取标签内容
        /// </summary>
        public override void GetContentFromDB()
        {
            bool isSelect = false;
            string templatePath =_templatepath ;
            if (!string.IsNullOrEmpty(templatePath))
            {
                int pCount = templatePath.IndexOf('/', 26);
                if (pCount > 0)
                {
                    templatePath = templatePath.Substring(0, pCount);
                }
            }
            string path =HttpContext.Current.Server.MapPath("~//" + templatePath + "/label/label.xml");
            if (File.Exists(path))
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(path);
                XmlNodeList nodeList = xmlDoc.SelectSingleNode("templetelabel").ChildNodes;//获取templetelabel节点的所有子节点
                if (nodeList.Count > 0)
                {
                    foreach (XmlNode xn in nodeList)//遍历所有子节点
                    {
                        XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型
                        if (xe.GetAttribute("Name") == _LabelName)
                        {
                            isSelect = true;
                            XmlNodeList nls = xe.ChildNodes;//继续获取xe子节点的所有子节点
                            foreach (XmlNode xn1 in nls)//遍历
                            {
                                XmlElement xe2 = (XmlElement)xn1;//转换类型
                                if (xe2.Name == "content")//如果找到
                                {
                                    LabelContent = xe2.InnerText;
                                    break;//找到后退出
                                }
                            }
                        }
                    }
                }
            }
            if (!isSelect)
            {
                string systemPath = HttpContext.Current.Server.MapPath("~//" + templatePath + "/label/systemlabel.xml");
                if (File.Exists(systemPath))
                {
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.Load(systemPath);
                    XmlNodeList nodeList = xmlDoc.SelectSingleNode("templetelabel").ChildNodes;//获取templetelabel节点的所有子节点
                    if (nodeList.Count > 0)
                    {
                        foreach (XmlNode xn in nodeList)//遍历所有子节点
                        {
                            XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型
                            if (xe.GetAttribute("Name") == _LabelName)
                            {
                                isSelect = true;
                                XmlNodeList nls = xe.ChildNodes;//继续获取xe子节点的所有子节点
                                foreach (XmlNode xn1 in nls)//遍历
                                {
                                    XmlElement xe2 = (XmlElement)xn1;//转换类型
                                    if (xe2.Name == "content")//如果找到
                                    {
                                        LabelContent = xe2.InnerText;
                                        break;//找到后退出
                                    }
                                }
                            }
                        }
                    }
                }
            }


            //YXShop.BLL.Templates.Label bll = new YXShop.BLL.Templates.Label();
            //YXShop.Model.Templates.Label model = bll.GetModelName(_LabelName);
            //if (model != null)
            //{
            //    LabelContent = model.Content;
            //}

        }
        /// <summary>
        /// 从数据库获取标签内容并返回内容
        /// </summary>
        /// <returns></returns>
        public override string GetLabelContent()
        {
            bool isSelect = false;
            string templatePath = _templatepath;
            if (!string.IsNullOrEmpty(templatePath))
            {
                int pCount = templatePath.IndexOf('/', 26);
                if (pCount > 0)
                {
                    templatePath = templatePath.Substring(0, pCount);
                }
            }
            string path = HttpContext.Current.Server.MapPath("~//" + templatePath + "/label/label.xml");
            if (File.Exists(path))
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(path);
                XmlNodeList nodeList = xmlDoc.SelectSingleNode("templetelabel").ChildNodes;//获取templetelabel节点的所有子节点
                if (nodeList.Count > 0)
                {
                    foreach (XmlNode xn in nodeList)//遍历所有子节点
                    {
                        XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型
                        if (xe.GetAttribute("Name") == _LabelName)
                        {
                            isSelect = true;
                            XmlNodeList nls = xe.ChildNodes;//继续获取xe子节点的所有子节点
                            foreach (XmlNode xn1 in nls)//遍历
                            {
                                XmlElement xe2 = (XmlElement)xn1;//转换类型
                                if (xe2.Name == "content")//如果找到
                                {
                                    LabelContent = xe2.InnerText;
                                    break;//找到后退出
                                }
                            }
                        }
                    }
                }
            }
            if (!isSelect)
            {
                string systemPath = HttpContext.Current.Server.MapPath("~//" + templatePath + "/label/systemlabel.xml");
                if (File.Exists(systemPath))
                {
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.Load(systemPath);
                    XmlNodeList nodeList = xmlDoc.SelectSingleNode("templetelabel").ChildNodes;//获取templetelabel节点的所有子节点
                    if (nodeList.Count > 0)
                    {
                        foreach (XmlNode xn in nodeList)//遍历所有子节点
                        {
                            XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型
                            if (xe.GetAttribute("Name") == _LabelName)
                            {
                                isSelect = true;
                                XmlNodeList nls = xe.ChildNodes;//继续获取xe子节点的所有子节点
                                foreach (XmlNode xn1 in nls)//遍历
                                {
                                    XmlElement xe2 = (XmlElement)xn1;//转换类型
                                    if (xe2.Name == "content")//如果找到
                                    {
                                        LabelContent = xe2.InnerText;
                                        break;//找到后退出
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return LabelContent;
            //YXShop.BLL.Templates.Label bll = new YXShop.BLL.Templates.Label();
            //YXShop.Model.Templates.Label model = bll.GetModelName(_LabelName);
            //if (model != null)
            //{
            //    LabelContent = model.Content;
            //}
            //return LabelContent;
        }

        public override void SetLabelContent(string labelContent)
        {
            LabelContent = labelContent;
        }
        /// <summary>
        /// 解析自定义标签内容
        /// </summary>
        protected void ParseLabelConetent()
        {
            string pattern = @"\[YX:unLoop,[^\]]+\][\s\S]*?\[/YX:unLoop\]|\[YX:Loop,[^\]]+\][\s\S]*?\[/YX:Loop\]";
            Regex reg = new Regex(pattern, RegexOptions.Compiled);
            string content = LabelContent;
            Match m = reg.Match(content);
            while (m.Success)
            {
                string mass = m.Value.Trim();
                LabelParse labelparse = new LabelParse(mass);
                labelparse.ParseContent();
                string s = labelparse.Parse();
                content = content.Replace(mass, s);
                m = reg.Match(content);
            }
            this._FinalHtmlCode = content;
        }

        /// <summary>
        /// 生成最终的HTML代码
        /// </summary>
        public override void MakeHtmlCode()
        {
            ParseLabelConetent();
        }
    }
}
 
